ggplot2 is an R-package, which was developed especially for data visualization. In contrast to many other tools for graphics creation, ggplot2 has its own grammar. This reflects a special philosophy, according to which graphics can be created. This “grammar” is taken from the book The grammar of graphics (Wilkinson 2005). That’s the reason why ggplot2 is called like that: the gg stands for grammar of graphics.
ggplot2 is one of the most used packages in R and has an extremely active community behind it. the ggplot2 mailing list has over 7,000 members and there is a very active Stack Overflow community, with nearly 10,000 questions tagged with ggplot2.
#install.packages("ggplot2")
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.3
citation("ggplot2")
##
## To cite ggplot2 in publications, please use:
##
## H. Wickham. ggplot2: Elegant Graphics for Data Analysis.
## Springer-Verlag New York, 2016.
##
## A BibTeX entry for LaTeX users is
##
## @Book{,
## author = {Hadley Wickham},
## title = {ggplot2: Elegant Graphics for Data Analysis},
## publisher = {Springer-Verlag New York},
## year = {2016},
## isbn = {978-3-319-24277-4},
## url = {https://ggplot2.tidyverse.org},
## }
In my opinion, this grammar forces you to think about what you want to say with the graphic. Compared to many other tools where you have to use predefined functions, ggplot2 offers maximum flexibility. The idea behind it is that graphics are put together piece by piece from different modules.
Without predefined functions? Sounds complicated!
In fact, ggplot2 has a set of different core principles that are recurrent or interchangeable. Basically these are:
This workshop will guide you through the basic principles of the package. And you will be able to produce good looking graphics for yourself in the end ;)
We will use some further packages in this workshop. ggplot2 is part of the tidyverse and works pretty well together with other packages from it. So we will use the dplyr-package sometimes to prepare the data accordingly.
Furthermore, we will use the gapminder dataset from the gapminder package. And finally we load (or install) the package viridis, which gives us basically another color scheme from the default one, which is good for publications (preserves differences between colors also when you print it in black/white-mode) and for color-blind-people (note: the author sometimes can’t seperate between green and red. Please don’t exclude people like him from exploring your beautiful analyses).
Let’s take a look into the gapminder dataset.
data(gapminder)
gapminder
## # A tibble: 1,704 x 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
## 7 Afghanistan Asia 1982 39.9 12881816 978.
## 8 Afghanistan Asia 1987 40.8 13867957 852.
## 9 Afghanistan Asia 1992 41.7 16317921 649.
## 10 Afghanistan Asia 1997 41.8 22227415 635.
## # ... with 1,694 more rows
As a first step, we extract only the data from the year 2007 from the dataset. We will use the filter-function of dplyr.
gapminder_2007 <- gapminder %>% filter(year==2007)
gapminder_2007
## # A tibble: 142 x 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 2007 43.8 31889923 975.
## 2 Albania Europe 2007 76.4 3600523 5937.
## 3 Algeria Africa 2007 72.3 33333216 6223.
## 4 Angola Africa 2007 42.7 12420476 4797.
## 5 Argentina Americas 2007 75.3 40301927 12779.
## 6 Australia Oceania 2007 81.2 20434176 34435.
## 7 Austria Europe 2007 79.8 8199783 36126.
## 8 Bahrain Asia 2007 75.6 708573 29796.
## 9 Bangladesh Asia 2007 64.1 150448339 1391.
## 10 Belgium Europe 2007 79.4 10392226 33693.
## # ... with 132 more rows
What is meant by modular structure becomes clear here: The basic structure of the graphic is already called by the ggplot command. With the argument aes (aesthetics) you can already define basic options of the graphic. In this case the x-axis should always be the gdp per capita and the y-axis the life expectancy. We pass the data set gapminder_2007 as data.
Thus ggplot already creates the axes. The data is already linked to the graph, but ggplot2 does not yet know the “type” of the graph (the geometric figures).
plot(gapminder_2007)
ggplot(data = gapminder_2007)
ggplot(data = gapminder_2007, aes(x = gdpPercap, y = lifeExp))
The so-called geoms can be used to specify which type of graphic should be displayed. In this case a geom_point as a point graph. ggplot now uses the x and y values and presents them as points.
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp))+
geom_point()
Often you want to draw trend lines through point clouds. This can be done in ggplot through the geom_smooth. This puts a regression line (and confidence intervals) through the data. The option method='lm' produces a linear approximation.
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp))+
geom_point()+
geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp))+
geom_point()+
geom_smooth(method = "lm")
The ordering of the geoms matter. “Later” geoms, will be added “on top” of the graph.
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp))+
geom_point(size = 3)+
geom_smooth(size = 2, method = "loess")
vs.
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp))+
geom_smooth(size = 2, method = "loess")+
geom_point(size = 3)
By adding another geom - geom_line for line graphs - the modular design of ggplot2-graphs becomes clearer. Values that follow each other on the x-axis are linked together. In this case, of course, connecting the points makes little sense.
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp))+
geom_point()+
geom_line()
pop_by_year <- gapminder %>% group_by(year) %>% summarise(worldpop = sum(pop, na.rm = TRUE)/1000000000)
ggplot(pop_by_year, aes(x = year, y = worldpop))+
geom_col()
ggplot(gapminder_2007, aes(x = gdpPercap))+
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(gapminder_2007, aes(x = gdpPercap))+
geom_density(fill = "blue", alpha = .3)
Black Line: Median, Box: 25% and 75%. Half of the distribution inside the box. whiskers: Additional countries Dots: Outliers (out of 95%)
ggplot(gapminder_2007, aes(x = continent, y = lifeExp)) +
geom_point()
ggplot(gapminder_2007, aes(x = continent, y = lifeExp)) +
geom_boxplot()
A fast and good overview over different geoms is the ggplot2-cheatsheet
So far we only had x and y. With aesthetics (aes), however, even more features can be controlled, such as color, groups, dot size, etc. A legend is added automatically.
The following is important: The option color could also be specified outside of aes(). Then you would color all points the same, e.g. in blue. By using it inside the aes() you can specify another variable of the data set and ggplot will use different colors based on this variable: here different colors for different continents.
ggplot(gapminder_2007) +
geom_point(aes(x = gdpPercap, y = lifeExp, color = continent)) +
scale_x_log10()
ggplot(gapminder_2007) +
geom_point(aes(x = gdpPercap, y = lifeExp), color = "blue") +
scale_x_log10()
Here the point size is set depending on the population size of the country. A second line within the aes does not matter to the R-Code. After commas there is even some nice indention, so you can see to which function the options belong.
ggplot(gapminder_2007, aes(x = gdpPercap,
y = lifeExp,
color = continent,
size = pop)) +
geom_point() +
scale_x_log10()
by_year_continent <- gapminder %>%
group_by(year, continent) %>%
summarize(totalPop = sum(as.numeric(pop)),
meanLifeExp = mean(lifeExp))
by_year_continent
## # A tibble: 60 x 4
## # Groups: year [12]
## year continent totalPop meanLifeExp
## <int> <fct> <dbl> <dbl>
## 1 1952 Africa 237640501 39.1
## 2 1952 Americas 345152446 53.3
## 3 1952 Asia 1395357351 46.3
## 4 1952 Europe 418120846 64.4
## 5 1952 Oceania 10686006 69.3
## 6 1957 Africa 264837738 41.3
## 7 1957 Americas 386953916 56.0
## 8 1957 Asia 1562780599 49.3
## 9 1957 Europe 437890351 66.7
## 10 1957 Oceania 11941976 70.3
## # ... with 50 more rows
Alright, next plot is a bit weird. Let’s separate the different continents by color. ggplot2 now automatically groups the lines and the plot makes much more sense now.
ggplot(by_year_continent, aes(x = year, y = totalPop)) +
geom_point() +
geom_line() +
expand_limits(y = 0)+
scale_color_viridis(discrete = T)
ggplot(by_year_continent, aes(x = year, y = totalPop, color = continent)) +
geom_point() +
geom_line() +
expand_limits(y = 0)+
scale_color_viridis(discrete = T)
Some geoms provide the group-option. If we like every line in black. Downside from it: We don’t know, which continent is which.
ggplot(by_year_continent, aes(x = year, y = totalPop, group = continent)) +
geom_point() +
geom_line() +
expand_limits(y = 0)+
scale_color_viridis(discrete = T)
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp))+
geom_point()
Problem: Many countries on the left, with very low gdp percap Possible solution: Log Scale (modular structure - adding a “module” log_scale)
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp))+
geom_point()+
scale_x_log10()
ggplot(gapminder_2007, aes(x = pop, y = gdpPercap))+
geom_point()+
scale_x_log10()+
scale_y_log10()
Faceting describes a division of the graphic into “subgraphs”. Again, a distinction is made according to the levels in a certain variable (here again continent).
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp)) +
geom_point() +
scale_x_log10() +
facet_wrap(~ continent)
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, size = pop))+
geom_point()+
scale_x_log10()+
facet_wrap(~ year)
by_year <- gapminder %>%
group_by(year) %>%
summarize(totalPop = sum(as.integer(pop)),
meanLifeExp = mean(lifeExp))
by_year
## # A tibble: 12 x 3
## year totalPop meanLifeExp
## <int> <dbl> <dbl>
## 1 1952 2406957150 49.1
## 2 1957 2664404580 51.5
## 3 1962 2899782974 53.6
## 4 1967 3217478384 55.7
## 5 1972 3576977158 57.6
## 6 1977 3930045807 59.6
## 7 1982 4289436840 61.5
## 8 1987 4691477418 63.2
## 9 1992 5110710260 64.2
## 10 1997 5515204472 65.0
## 11 2002 5886977579 65.7
## 12 2007 6251013179 67.0
ggplot(by_year, aes(x = year, y = totalPop/1000000000)) +
geom_point()
Here the y-axis does not contain the 0 (most often a major mistake). Therefore we have to edit and adjust the scale. Again this can be done by a new “module”.
ggplot(by_year, aes(x = year, y = totalPop/1000000000)) +
geom_point() +
expand_limits(y = 0)
Sometimes it is necessary to change the scale in particular, as with scale_x_log10. But also colors etc. can be changed via scale. There are several functions for changing scales. They all start with `scale_’ and then followed by what you want to change (color, size, etc.)
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
scale_x_log10() +
scale_color_discrete(name = "Continent")+
scale_y_continuous(limits = c(0,100), breaks = seq(0,100,10))
At this point the viridis-package comes in. Some addon-packages to ggplot2 provide their own “scale-options”.
Use the color scales in this package to make plots that are pretty, better represent your data, easier to read by those with colorblindness, and print well in grey scale. Viridis-Package
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
scale_x_log10() +
scale_color_viridis_d(name = "Continent")+
scale_y_continuous(limits = c(0,100), breaks = seq(0,100,10))
Basically, aes are linking the data to the graph, while scales decide how the aesthetics will look like.
Sometimes it is necessary to change the orientation of the plot. The only time I use one of these options, is the coord_flip. But there are some more coords-options.
gapminder_2007 %>% top_n(30) %>%
ggplot(aes(x = country, y = lifeExp))+
geom_col()
## Selecting by gdpPercap
gapminder_2007 %>% top_n(30) %>%
ggplot(aes(x = country, y = lifeExp))+
geom_col()+
coord_flip()
## Selecting by gdpPercap
Of course we also want to label our graphics.
ggplot(gapminder_2007, aes(x = continent, y = gdpPercap)) +
geom_boxplot() +
scale_y_log10() +
labs(title = "Comparing GDP per capita across continents",
x = "Continent",
y = "GDP per capita")
There are some predefined themes in ggplot. In addition, the ggthemes-package contains some more presets. This can make your graphics look like the Wall Street Journal, fivethirtyeight or good old excel.
#install.packages("ggthemes")
library(ggthemes)
All you must do is to add the theme to the ggplot-code-line.
ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
scale_x_log10()+
theme_bw()
But of course you can also create your own theme. There are hundreds of different setting options. The most common is probably that you want to remove the legend or change its position, but you can do whatever you like, basically.
ggplot(data = gapminder_2007, aes(x = continent, y = lifeExp, color = continent))+
geom_boxplot()+
theme(legend.position = "off")
ggplot(data = gapminder_2007, aes(x = continent, y = lifeExp, color = continent))+
geom_boxplot()+
theme(legend.position = "top",
axis.text.x = element_text(angle = 90),
axis.ticks.length = unit(50, "pt"),
panel.background = element_rect(fill = "darkblue", color = "darkblue"))
A strength of R are also interactive graphics. When called via ggplotly from the plotly-package, the graphic is already rudimentarily interactive. Labels etc. would have to be edited again. However, some functionability is already available.
#install.packages("plotly")
library(plotly)
p_scatter <- ggplot(gapminder_2007, aes(x = gdpPercap,
y = lifeExp,
color = continent,
size = pop)) +
geom_point() +
scale_x_log10()
p_scatter
ggplotly(p_scatter)
The gganimate-package extends the grammar of graphics as implemented by ggplot2 to include the description of animation. It does this by providing a range of new grammar classes that can be added to the plot object in order to customise how it should change with time.
#install.packages("gganimate")
library(gganimate)
p <- ggplot(gapminder) +
aes(x = gdpPercap, y = lifeExp, size = pop, color = continent) +
geom_point() +
scale_x_log10()+
guides(color = FALSE, size = FALSE)
p
p +
transition_states(year, 1, 0) +
ggtitle("{closest_state}")